<?php

namespace App\Library\Traits\Orm;

use App\Library\Logic\LogisticsLogic;
use App\Library\Logic\ShopExpressDeliveryLogic;
use App\Models\LogisticsModel;
use App\Models\PaymentModel;
use App\Models\PaymentRelatedModel;
use App\Models\ShopOrderExtModel;
use App\Models\ShopOrderGoodsModel;
use App\Models\ShopOrderReturnModel;
use App\Models\ShopUserModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

trait ShopOrderOrmTrait
{
    public function ext(): BelongsTo
    {
        return $this->belongsTo(ShopOrderExtModel::class, 'order_id', 'order_id');
    }

    public function goods(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id')
            ->leftJoin('shop_product', 'shop_product.product_id', 'shop_order_goods.product_id')->select([
                'id',
                'product_category_id_1',
                'order_id',
                'seller_id',
                'product_file_id',
                'spec_file_id',
                'shop_order_goods.product_id',
                'spec_id',
                'title',
                'spec',
                'quantity',
                'original_price',
                'special_price',
                'price',
                'discount',
                'discount_special',
                'discount_sub',
                'discount_coupon',
                'commission',
                'buy_will_id',
                'info'
            ]);
    }

    public function goodsKefu(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id')
            ->leftJoin('shop_product', 'shop_product.product_id', 'shop_order_goods.product_id')->select([
                'id',
                'product_category_id_1',
                'order_id',
                'seller_id',
                'product_file_id',
                'spec_file_id',
                'shop_order_goods.product_id',
                'spec_id',
                'title',
                'spec',
                'quantity',
                'original_price',
                'special_price',
                'price',
                'discount',
                'discount_special',
                'discount_sub',
                'discount_coupon',
                'commission',
                'buy_will_id',
            ]);
    }


    public function goodsSimple(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id')
            ->leftJoin('shop_product', 'shop_product.product_id', 'shop_order_goods.product_id')->select([
                'order_id',
                'product_file_id',
                'spec_file_id',
                'title',
                'spec',
                'quantity',
            ]);
    }

    public function goodShot(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id')->select(['id', 'order_id']);
    }

    public function paymentRelated(): BelongsTo
    {
        return $this->belongsTo(PaymentRelatedModel::class, 'order_id', 'object_id')
            ->where('object_type', $this->getTable())->where('status_pay', 1);
    }

    public function payment(): BelongsTo
    {
        return $this->belongsTo(PaymentModel::class, 'order_id', 'object_id')
            ->where('object_type', $this->getTable())->orderBy('id');
    }

    public function orderReturn(): HasOne
    {
        return self::hasOne(ShopOrderReturnModel::class, 'order_id', 'order_id')->orderByDesc('or_id');
    }

    public function express(): HasOne
    {
        return self::hasOne(ShopExpressDeliveryLogic::class, 'ed_source_id', 'order_id')
            ->where('ed_type', '1');
    }

    public function sellInfo(): BelongsTo
    {
        return self::BelongsTo(ShopUserModel::class, 'sell_user_id', 'user_id');
    }

    public function logistics(): HasMany
    {
        return $this->hasMany(LogisticsModel::class, 'object_id', 'order_id')->orderByDesc('id')->where('object_type', 1)
            ->where('type', 0);
    }

    public function oneLogistics(): HasOne
    {
        return $this->HasOne(LogisticsModel::class, 'object_id', 'order_id')->orderByDesc('id')->where('object_type', 1)
            ->where('type', 0);
    }

    public function packages(): HasMany
    {
        return $this->hasMany(LogisticsModel::class, 'object_id', 'order_id')->orderBy('id')->where('object_type', 1)->where('type', 0)
            ->select('serial_number', 'object_id', 'express_no');
    }

    public function messageGoods(): HasMany
    {
        return $this->hasMany(ShopOrderGoodsModel::class, 'order_id', 'order_id')->select([
            'product_file_id',
            'spec_file_id',
            'title',
            'spec',
            'quantity',
            'price',
            'buy_will_id',
        ]);
    }
}
